package com.xtck.ha.fault.modules.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author zhangxa
 */
@RestController
@RequestMapping(value = "/satellite")
@Api(tags = "卫星管理")
public class SatelliteController  {

    /**
     * 获取节点树左侧信第三方接口
     * @param pageNum  页码
     * @param pageSize 页数
     * @param xingType 卫星类型
     * @return 结果日志
     */
    @ApiOperation(value = "树结构(第三方接口)")
    @GetMapping("getLeftTreeByOther")
    public JSONObject getLeftTreeByOther(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                         @RequestParam String xingType) {
        try {
            // 拼接完整请求URL
            String baseUrl = "http://127.0.0.1:8099/health-mgr-back/v1.0/satellite/getLeftTreeByOther";
            String query = "?xingType="+xingType+"&pageNum="+pageNum+"&pageSize="+pageSize;
            String fullUrl = baseUrl + query;

            URL url = new URL(fullUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            int responseCode = conn.getResponseCode();
            System.out.println("响应码：" + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "UTF-8"));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            System.out.println("响应内容：");
            System.out.println(response.toString());

            return JSON.parseObject(response.toString());

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



}
